package hu.myai.indicators;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author bozbalint
 *
 */
public class RateOfChangeIndicator extends AbstractIndicator {

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public RateOfChangeIndicator(Double parameter) {
        super(parameter);
    }

    @Override
    public void init() {
    }

    /**
     * Here we calculate 12 days ROC (Rate of Change)<br>
     * A = Take Today's Close Price<br>
     * B = Take 12 days before Close Price<br>
     * ROC=[(A-B )/B]*100
     * */

    @Override
    protected IndicatorVO indicatorCalculation() {

        if (tickHistory.size() - 1 < parameter)
            return null;

        int intParameter = parameter.intValue();
        double beforeValue = tickHistory.get(intParameter - 1).getClose();

        double roc = (lastTick.getClose() - beforeValue) / beforeValue * 100;

        IndicatorVO indicator = new IndicatorVO();
        indicator.setValue(roc);
        indicator.setChanged(true);
        indicator.setDate(lastTick.getTime());

        log.debug(indicator.toString());
        return indicator;
    }
}
